﻿namespace PMS.Client.FrmRoleMenu
{
    using PMS.BLL;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public class FrmSetMenu : Form
    {
        private RoleMenuBLL autorityBLL = new RoleMenuBLL();
        private List<RoleMenu> autorityList = null;
        private Button btnCancel;
        private Button btnOK;
        private IContainer components = null;
        private MenuBLL menuBLL = new MenuBLL();
        private List<PMS.Model.Menu> menuList = null;
        private Role role = null;
        private TreeView treeView1;

        public FrmSetMenu(int roleid)
        {
            this.InitializeComponent();
            this.RoleID = roleid;
            this.role = new RoleBLL().GetModel(roleid);
            this.menuList = this.menuBLL.GetList();
            this.autorityList = this.autorityBLL.GetList(roleid);
            this.autorityList = (this.autorityList == null) ? new List<RoleMenu>() : this.autorityList;
            this.ReturnMenuTreeBind("", this.treeView1.Nodes);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.autorityBLL.DeleteList(this.RoleID);
            this.SaveTree(this.treeView1.Nodes);
            SyslogBLL.Write("修改角色", "修改角色" + this.role.RoleName + "权限菜单");
            base.Close();
        }

        private void CheckChildNode(TreeNode node)
        {
            if (node.Nodes.Count > 0)
            {
                foreach (TreeNode node2 in node.Nodes)
                {
                    node2.Checked = true;
                    this.CheckChildNode(node2);
                }
            }
        }

        private void CheckParentNode(TreeNode node)
        {
            if (node.Parent != null)
            {
                node.Parent.Checked = true;
                this.CheckParentNode(node.Parent);
            }
            else
            {
                node.Checked = true;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmSetMenu));
            this.treeView1 = new TreeView();
            this.btnCancel = new Button();
            this.btnOK = new Button();
            base.SuspendLayout();
            this.treeView1.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            this.treeView1.BorderStyle = BorderStyle.FixedSingle;
            this.treeView1.CheckBoxes = true;
            this.treeView1.Dock = DockStyle.Top;
            this.treeView1.FullRowSelect = true;
            this.treeView1.HideSelection = false;
            this.treeView1.ItemHeight = 0x10;
            this.treeView1.Location = new Point(0, 0);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new Size(0x160, 0x1c1);
            this.treeView1.TabIndex = 1;
            this.treeView1.AfterCheck += new TreeViewEventHandler(this.treeView1_AfterCheck);
            this.btnCancel.BackColor = Color.White;
            this.btnCancel.FlatStyle = FlatStyle.Popup;
            this.btnCancel.Location = new Point(0xd0, 0x1d3);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 9;
            this.btnCancel.Text = "关闭";
            this.btnCancel.UseVisualStyleBackColor = false;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnOK.BackColor = Color.White;
            this.btnOK.FlatStyle = FlatStyle.Popup;
            this.btnOK.Location = new Point(0x53, 0x1d3);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new Size(0x4b, 0x17);
            this.btnOK.TabIndex = 8;
            this.btnOK.Text = "保存";
            this.btnOK.UseVisualStyleBackColor = false;
            this.btnOK.Click += new EventHandler(this.btnOK_Click);
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            base.ClientSize = new Size(0x160, 0x1f6);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnOK);
            base.Controls.Add(this.treeView1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FrmSetMenu";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "角色菜单权限分配";
            base.ResumeLayout(false);
        }

        private void ReturnMenuTreeBind(string menuCode, TreeNodeCollection treeNodes)
        {
            List<PMS.Model.Menu> list = this.menuList.FindAll(f => f.MenuCode.Substring(0, f.MenuCode.Length - 2) == menuCode).ToList<PMS.Model.Menu>();
            if (list.Count > 0)
            {
                TreeNode node = null;
                using (List<PMS.Model.Menu>.Enumerator enumerator = list.GetEnumerator())
                {
                    Predicate<RoleMenu> match = null;
                    PMS.Model.Menu menu;
                    while (enumerator.MoveNext())
                    {
                        menu = enumerator.Current;
                        node = new TreeNode {
                            Text = menu.MenuName,
                            ImageIndex = 0,
                            Name = menu.MenuID.ToString(),
                            Tag = menu.MenuCode
                        };
                        if (match == null)
                        {
                            match = f => f.MenuCode == menu.MenuCode;
                        }
                        node.Checked = this.autorityList.Exists(match);
                        node.Expand();
                        treeNodes.Add(node);
                        this.ReturnMenuTreeBind(menu.MenuCode, node.Nodes);
                    }
                }
            }
        }

        private void SaveTree(TreeNodeCollection nodes)
        {
            RoleMenu model = null;
            foreach (TreeNode node in nodes)
            {
                if (node.Checked && (node.Tag.ToString().Trim() != ""))
                {
                    model = new RoleMenu {
                        MenuCode = node.Tag.ToString(),
                        RoleID = this.RoleID
                    };
                    this.autorityBLL.Add(model);
                }
                this.SaveTree(node.Nodes);
            }
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            this.treeView1.AfterCheck -= new TreeViewEventHandler(this.treeView1_AfterCheck);
            if (e.Node.Checked)
            {
                if (e.Node.Parent == null)
                {
                    this.CheckChildNode(e.Node);
                }
                else
                {
                    this.CheckParentNode(e.Node);
                    this.CheckChildNode(e.Node);
                }
            }
            else if (e.Node.Parent == null)
            {
                this.UncheckNode(e.Node);
            }
            else
            {
                this.UncheckNode(e.Node);
            }
            this.treeView1.AfterCheck += new TreeViewEventHandler(this.treeView1_AfterCheck);
        }

        private void UncheckNode(TreeNode node)
        {
            if (node.Nodes.Count > 0)
            {
                foreach (TreeNode node2 in node.Nodes)
                {
                    node2.Checked = false;
                    this.UncheckNode(node2);
                }
            }
        }

        private void UncheckParentNode(TreeNode node)
        {
            int num = 0;
            if (node.Parent != null)
            {
                foreach (TreeNode node2 in node.Parent.Nodes)
                {
                    if (!node2.Checked)
                    {
                        num++;
                    }
                }
                if (node.Parent.Nodes.Count == num)
                {
                    node.Parent.Checked = false;
                    this.UncheckParentNode(node.Parent);
                }
            }
        }

        private int RoleID { get; set; }
    }
}

